<template>
  <div class="banner-add">

    <div class="createPost-main-container">
      <el-form ref="form" label-width="120px" :rules="rules" :model="form">

        <el-form-item label="资讯标题" prop="title">
          <el-input v-model="form.title" type="text" maxlength="60" show-word-limit />
        </el-form-item>

        <el-form-item label="资讯摘要" prop="summary">
          <el-input v-model="form.summary" type="text" maxlength="200" show-word-limit />
        </el-form-item>

        <el-form-item label="资讯类型">
          <el-select v-model="form.informationType.id" filterable placeholder="跳转类型">
            <el-option
              v-for="(type, index) in typeList"
              :key="index"
              :label="type.name"
              :value="type.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="资讯正文" prop="details">
          <tinymce-editor
            ref="myEditor"
            upload-model="news"
            show-hyperlink-btn
          />

        </el-form-item>

      </el-form>
    </div>

    <sticky :class-name="'cms-sub-navbar '+ajaxStatus">
      <el-button :loading="ajaxStatus" style="margin-left: 10px;" type="success" @click="submitForm('up')">
        发布
      </el-button>
      <el-button :loading="ajaxStatus" @click="reset()">
        取消
      </el-button>
    </sticky>

  </div>
</template>

<script>
import { postData, getEditData } from '@/api/news/index'
import Sticky from '@/components/Sticky'
import { getAllNewsTypeLink } from '@/api/common'
import TinymceEditor from 'components/ueditor'

const defaultForm = {
  title: '',
  summary: '',
  content: '',
  informationType: {
    id: ''
  }
}

const typeList = [
  {
    type: '电工培训',
    value: '电工培训'
  }
]

export default {
  components: { Sticky, TinymceEditor },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    tabName: {
      type: String,
      default: ''
    }
  },
  data() {
    const checkDetails = (rule, value, callback) => {
      if (!this.form.details) {
        return callback(new Error('请填入资讯正文'))
      }
      callback()
    }
    return {
      ajaxStatus: false,
      form: Object.assign({}, defaultForm),
      typeList,
      rules: {
        title: { required: true, message: '请输入小于60个字的标题', trigger: 'blur', min: 1, max: 60 },
        summary: { required: true, message: '请输入小于200个字的资讯摘要', trigger: 'blur', min: 1, max: 200 },
        details: { required: true, validator: checkDetails }
      }
    }
  },
  computed: {
  },
  created() {
    if (this.isEdit) {
      const id = this.$route.query && this.$route.query.id
      this.fetchData(id)
    }
    this.getTypeList()
  },
  methods: {

    // 获取编辑数据
    fetchData(id) {
      getEditData(id).then(re => {
        const { data } = re
        this.form = Object.assign(this.form, data)
        this.$refs.myEditor.edit(data.details)
      })
    },
    getTypeList() {
      getAllNewsTypeLink().then(re => {
        const { data } = re
        this.typeList = data
      })
    },
    /**
     * 保存、发布接口
     *
     * id：保存为空，编辑传列表id
     */
    submitForm(status) {
      if (this.ajaxStatus) {
        return
      }
      this.ajaxStatus = true
      this.form.details = this.$refs.myEditor.onEditorChange()
      this.$refs.form.validate(valid => {
        if (valid) {
          this.form.status = status
          postData(this.form)
            .then(re => {
              this.ajaxStatus = false
              this.$notify({
                title: '成功',
                message: '操作成功',
                type: 'success',
                duration: 2000
              })
              this.reset()
            })
            .catch(() => {
              this.ajaxStatus = false
            })
        } else {
          this.ajaxStatus = false
          this.$message.error('请正确填写完整信息后再发布')
          return false
        }
      })
    },

    reset() {
      // 编辑发布后直接关闭tab跳转至列表页面
      if (+this.tabName > 2) {
        this.$emit('removeTab', this.tabName, '1')
        return
      }
      this.imgData = {
        myId: Math.random(),
        path: 'news',
        imgUrl: '',
        ImgSize: 10000,
        text: '图片大小不得超过10000K，支持png/jpg格式'
      }
      this.form = Object.assign({}, defaultForm)
      this.$refs['form'].resetFields()
      this.$refs.myEditor.edit('')
      this.$emit('removeTab', this.tabName)
    }

  }
}
</script>
<style lang="scss" scoped>
.href-remark{
  color: #5e6d82;
  .red {
    background-color: #fdf6ec;
    color: #e6a23c;
  }
}
.logo-card{
  margin-bottom: 10px;
}
</style>

